package com.ivanceras.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.ivanceras.model.Choice;

public class ChoiceView {
	public HorizontalPanel view = new HorizontalPanel();
	public HorizontalPanel edit = new HorizontalPanel();
	
	public Choice choice;
	
	private RadioButton radioView = new RadioButton("");
	private HTML textView = new HTML();
	
	private RadioButton radioEdit = new RadioButton("");
	private TextBox textEdit = new TextBox();
	
	public ChoiceView(){
		initLayout();
	}
	public void initLayout(){
		view.add(radioView);
		view.add(textView);

		edit.add(radioEdit);
		edit.add(textEdit);
	}
	public void initHandler(){
		textEdit.addKeyUpHandler(new KeyUpHandler(){

			@Override
			public void onKeyUp(KeyUpEvent event) {
				choice.text = textEdit.getText();
			}
			
		});
		radioEdit.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				//TODO: need to be in QuestionEdit since assigning of correct answer is appropriate in there
				choice.isCorrect = radioEdit.getValue(); 
			}
			
		});
		radioView.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				choice.isAnswer = radioView.getValue();
			}
			
		});
	}
	public void setChoice(Choice ch){
		choice = ch;
		textEdit.setText(ch.getText());
		textView.setHTML(ch.getText());
	}
}
